/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.collections.FXCollections
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.ChoiceBox
 *  javafx.scene.control.Label
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Stage
 *  javafx.util.StringConverter
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.pay;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.SalesReceiptsUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.SpringUtil;
import com.gys.common.util.ValidationUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.pay.PayController;
import com.gys.data.deviceCheck.GsdcStatus;
import com.gys.data.example.GetLoginStoreOutData;
import com.gys.view.pay.DyqView;
import de.felixroske.jfxsupport.FXMLController;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class DyqController
extends BaseController
implements Initializable {
    @FXML
    private AnchorPane pane;
    @FXML
    private Label tips;
    @FXML
    public TextField code;
    @FXML
    public TextField amount;
    @FXML
    public ChoiceBox<GsdcStatus> reason;
    @Autowired
    private PayController payController;
    private String maxAmount;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        this.amount.requestFocus();
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                DyqView view = SpringUtil.getBean(DyqView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            DyqController.this.sureAction();
                        } else if (e.getCode() == KeyCode.ESCAPE) {
                            DyqController.this.cancelAction();
                        }
                        e.consume();
                    }
                });
            }
        }, 200L);
        ArrayList list = new ArrayList();
        this.reason.setItems(FXCollections.observableArrayList(list));
        this.reason.getItems().addAll(this.getAllStatus());
        this.reason.converterProperty().set(new StringConverter<GsdcStatus>(){

            public String toString(GsdcStatus object) {
                return object.getName();
            }

            public GsdcStatus fromString(String string) {
                return null;
            }
        });
        this.reason.setValue(new GsdcStatus("1", "\u4fc3\u9500\u62b5\u6263"));
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("prcStore", ((GetLoginStoreOutData)CacheUtil.get("store")).getStoCode());
        HttpJson rsp = SalesReceiptsUtil.getStoreDyqMaxAmount(param);
        if (!rsp.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", rsp.getMsg());
            alert.showAndWait();
            return;
        }
        this.maxAmount = (String)JSON.parseObject((String)rsp.getMsg(), String.class);
    }

    @FXML
    private void sureAction() {
        if (ObjectUtil.isNotEmpty(this.code.getText()) && !ValidationUtil.checkPositiveInteger(this.code.getText())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u7528\u5238\u7f16\u7801\u4e0d\u5408\u6cd5\uff01");
            alert.showAndWait();
            return;
        }
        if (ObjectUtil.isEmpty(this.amount.getText())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u62b5\u7528\u91d1\u989d\uff01");
            alert.showAndWait();
            return;
        }
        if (!ValidationUtil.checkPositiveInteger(this.amount.getText())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u7528\u91d1\u989d\u4e0d\u5408\u6cd5\uff01");
            alert.showAndWait();
            return;
        }
        if (new BigDecimal(this.amount.getText()).compareTo(new BigDecimal(this.maxAmount)) > 0) {
            this.tips.setVisible(true);
            return;
        }
        if (new BigDecimal(this.amount.getText()).compareTo(new BigDecimal(this.payController.getDsAmount().getText())) > 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u7528\u91d1\u989d\u4e0d\u80fd\u5927\u4e8e\u5f85\u4ed8\u91d1\u989d");
            alert.showAndWait();
            return;
        }
        this.payController.refreshAmount("9003", this.amount.getText());
        this.payController.setDyqCode(this.code.getText());
        this.payController.setDyqAmount(this.amount.getText());
        this.payController.setDyqReason(((GsdcStatus)this.reason.getValue()).getName());
        this.tips.setVisible(false);
        this.pane.getScene().getWindow().hide();
    }

    @FXML
    private void cancelAction() {
        this.tips.setVisible(false);
        this.code.setText("");
        this.amount.setText("");
        this.reason.setValue(new GsdcStatus("1", "\u4fc3\u9500\u62b5\u6263"));
        this.pane.getScene().getWindow().hide();
    }

    private List<GsdcStatus> getAllStatus() {
        ArrayList<GsdcStatus> list = new ArrayList<GsdcStatus>();
        list.add(new GsdcStatus("1", "\u4fc3\u9500\u62b5\u6263"));
        list.add(new GsdcStatus("2", "\u6548\u671f\u62b5\u6263"));
        list.add(new GsdcStatus("3", "\u7834\u635f\u62b5\u6263"));
        return list;
    }
}

